<?php


namespace app\api\controller;


use app\api\model\DiaryTypeModel;
use app\api\model\UserDiaryModel;
use app\common\controller\Api;

class Diary extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function index()
    {

        $days = UserDiaryModel::where(['user_id'=>$this->auth->id])->group('day')->column('day');

        $arr = [];
        foreach ($days as $key => $val){
            $arr[$key]['time'] = $val;
            $val_arr = explode('-',$val);
            $val = $val_arr[0].'年'.$val_arr[1].'月'.$val_arr[2].'日';
            $arr[$key]['time_text'] = $val;
        }

        $this->success('请求成功',$arr);
    }

    public function typeList()
    {
        $model = new DiaryTypeModel();

        $day = input('post.day');

        $type_id_arr = UserDiaryModel::where(['user_id'=>$this->auth->id,'day'=>$day])->column('type_id');

        $list = $model->where('id','in',$type_id_arr)->order('weigh desc')->select();

        $this->success('请求成功',$list);
    }

    public function diaryList()
    {
        $type = input('post.type');

        if(empty($type)){
            $this->error('请传入日记类型');
        }
        $day = input('post.day');
        if(empty($day)){
            $this->error('请传入日期');
        }

        $list = UserDiaryModel::where(['user_id'=>$this->auth->id,'day'=>$day,'type_id'=>$type])->order('id desc')->select();

        $this->success('请求成功',$list);
    }
}